$(function() {
    var data = [];

	var grid = $("#grid").bootgrid(
			{
				post: function () {
			        return {
			        	nickname: $('#nickName').val()
			        } ;
			    },
				ajax: true,
			//	pagination: 3,
				selection: true,
				multiSelect: true,
				sorting: true,
				multiSort: true,
				url: ctx + "/admin/admin/dictionary/page",
				labels: {
					noResults: "对不起，暂无数据！",
					loading: "正在加载数据，请稍后...",
					refresh: "重新加载数据",
					search: "搜索"
				},
			    formatters: {
			        "commands": function(column, row)
			        {
			        	var commands = "<button type=\"button\" class=\"btn btn-xs btn-default glyphicon glyphicon-edit\" data-row-id=\"" + row.id + "\"></button> " ;
			        	commands += "<button type=\"button\" class=\"btn btn-xs btn-default glyphicon glyphicon-trash\" data-row-id=\"" + row.id + "\"></button>" ;
			        	
			        	return commands ;
			        }
			    }
			}).on("loaded.rs.jquery.bootgrid",function(e) {
				
			    grid.find(".glyphicon-edit").on("click", function(e){
			    	var id = $(this).data("row-id") ;
			    	$('#layoutModal').modal({
						backdrop: 'static' ,
						remote: ctx+'/admin/admin/dictionary/modify/'+id ,
						show: true
					}) ;
			        
			    }).end().find(".glyphicon-trash").on("click", function(e){
			    	
			        alert("You pressed delete on row: " + $(this).data("row-id"));
			    });
			}).on("selected.rs.jquery.bootgrid", function(e, selectedRows) {
				var row;
				for ( var i = 0; i < selectedRows.length; i++) {
					row = selectedRows[i];
					data.push(row.id) ;
				}
			}).on("deselected.rs.jquery.bootgrid", function(e, deselectedRows) {
				var row;
				for ( var i = 0; i < deselectedRows.length; i++) {
					var dataIds = data.join(",") ;
					row = deselectedRows[i];
					dataIds += ',' ;
					dataIds = dataIds.replace(row.id+',', '') ;
					data = dataIds.substring(0,dataIds.length-1).split(',') ;
				}
				
			});
	/**添加新记录**/
	$("button[name=toCreate]").on("click", function() {
		
		$('#layoutModal').modal({
			backdrop: 'static' ,
			remote: ctx+'/admin/admin/dictionary/create',
			show: true
		}) ;
	});
	/**批量删除**/
	$("button[name=deleteBath]").on("click", function() {
		var msg = "<h4><span class='danger'>请选择要删除的数据.</span></h4>" ;
		if(!data.join(',')){
			$('#alertModal .modal-body').html(msg) ;
			$('#alertModal').modal({
				backdrop: 'static' ,
				show: true
			}) ;
			
			return false;
		}
		
		msg = "<h4><span class='danger'>您确定要删除吗?</span></h4>" ;
		$('#dialogModal .modal-body').html(msg) ;
		$('#dialogModal').modal({
			backdrop: 'static' ,
			show: true
		}) ;
		$('#dialogBtn').click(function(){
			XHZ.post(ctx + "/admin/admin/dictionary/delete",{ids: data.join(',')}, function(data){
				var msg = "操作成功" ;
				$('#dialogModal').modal('hide') ;
				if(data.code == 1){
					$('.actionBar .glyphicon-refresh').click() ;
				}else{
					msg = data.message ;
				}
				$('#alertModal .modal-body').html(msg) ;
				$('#alertModal').modal({
					backdrop: 'static' ,
					show: true
				}) ;
			}) ;
		}) ;
	});
	/**审核**/
	$("button[name=audit]").on("click", function() {
		
	});
	
});

